package com.example.demo.controller;

import com.example.demo.controller.data.ReturnData;
import com.example.demo.controller.vo.RegisterUser;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

//用户相关的接口
@RestController
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 获取当前用户的用户信息
     * @param request
     * @return
     */
    @GetMapping("getUser")
    public ReturnData getUser(HttpServletRequest request){
        String token = request.getHeader("token");//取Token
        return userService.getUser(token);
    }
    /**
     * 登录接口
     */

    @PostMapping("login")
    public ReturnData login(@RequestBody RegisterUser user){
        return userService.login(user.getUsername(),user.getPassword());
    }

    /**
     * 注册接口
     * @param user
     * @return
     */
    @PostMapping("register")
    public ReturnData register(@RequestBody RegisterUser user){
        return userService.register(user);
    }
    /**
     * 获取验证码接口
     * @param email
     * @return
     */
    @PostMapping("getCode")
    public ReturnData getCode(String email){
        return userService.getCode(email);
    }
    /**
     * 编辑用户信息接口
     * @return
     */
    @PostMapping("updateUserInfo")
    public ReturnData updateUserInfo(@RequestBody RegisterUser user,HttpServletRequest request){
        String token = request.getHeader("token");//取Token
        return userService.updateUserInfo(user,token);
    }

}
